<template>
  <div>
    <Header></Header>
    <div class="m-content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="摘要" prop="description">
          <el-input type="textarea" v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <mavon-editor v-model="ruleForm.content"></mavon-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">发表</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Header from "../components/Header.vue";
import Element from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
export default {
  name: "blogEdit",
  components: { Header },
  data() {
    return {
      ruleForm: {
        id: "",
        title: "",
        description: "",
        content: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 3,
            max: 25,
            message: "长度在 3 到 25 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入摘要", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
    };
  },
  created(){
      this.getBlogDetail();
  },
  methods: {
    //提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var vm = this;
          vm.$axios({
            method: "POST",
            url: "/blog/edit",
            data: vm.ruleForm,
            headers: {
              token: localStorage.getItem("token"),
            },
          }).then((res) => {
            this.$alert("操作成功", "提示", {
              confirmButtonText: "确定",
              callback: (action) => {
                vm.$router.push("/blogs");
              },
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取博客详细信息
    getBlogDetail() {
      var vm = this;
      var blogId = vm.$route.params.blogid
      if(blogId){
          vm.$axios({
              method:"get",
              url:"/blog/detail/"+blogId
          }).then(res=>{
              const blog = res.data.data;
              vm.ruleForm.id = blog.id;
              vm.ruleForm.title = blog.title;
              vm.ruleForm.description = blog.description;
              vm.ruleForm.content = blog.content;
          })
      }
    },
  },
};
</script>
<style scoped>
.m-content {
  text-align: center;
}
.demo-ruleForm {
  max-width: 960px;
}
</style>